package com.dykj.fireFighting.web.controller.GB26875Controller;

import com.dykj.fireFighting.web.service.DevHisSXinService;
import com.dykj.fireFighting.web.service.DevHisWaterService;
import com.dykj.fireFighting.web.utils.OneNetUtils;
import org.apache.dubbo.common.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping(value = "/onenet")
public class OnenetController {
    @Autowired
    private DevHisSXinService devHisSXinService;
    @Autowired
    private DevHisWaterService devHisWaterService;
    private static String token ="tssy20211213";//用户自定义token和OneNet第三方平台配置里的token一致
    //拓普瑞token
    private static String topriToken = "20211213";
    /**
     * 功能描述：第三方平台数据接收。<p>
     *           <ul>注:
     *               <li>1.OneNet平台为了保证数据不丢失，有重发机制，如果重复数据对业务有影响，数据接收端需要对重复数据进行排除重复处理。</li>
     *               <li>2.OneNet每一次post数据请求后，等待客户端的响应都设有时限，在规定时限内没有收到响应会认为发送失败。
     *                    接收程序接收到数据时，尽量先缓存起来，再做业务逻辑处理。</li>
     *           </ul>
     * @param request 数据消息
     * @return 任意字符串。OneNet平台接收到http 200的响应，才会认为数据推送成功，否则会重发。
     */
    @RequestMapping(value = "/receive",method = RequestMethod.POST)
    @ResponseBody
    public String receive(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
        System.out.println("data receive:  body String --- " +name);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
        System.out.println("data receive:  body Object --- " +obj);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignature(obj, token);
            if (dataRight){
                System.out.println("data receive: content" + obj.toString());
//                devHisSXinService.putDevHisSXinToMQ(obj.getMsg());
//                devHisWaterService.putDevHisWaterToMQ(obj.getMsg());
            }else {
                System.out.println("data receive: signature error");
            }

        }else {
            System.out.println("data receive: body empty error");
        }

        return "ok";
    }

    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     * @param msg 验证消息
     * @param nonce 随机串
     * @param signature 签名
     * @return msg值
     */

    @RequestMapping(value = "/receive", method = RequestMethod.GET)
    @ResponseBody
    public String check(@RequestParam(value = "msg") String msg,
                        @RequestParam(value = "nonce") String nonce,
                        @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
            return msg;
    }
//    @RequestMapping(value = "/indextest", method = RequestMethod.GET)
//    @ResponseBody
//    public void index(){
//        System.out.println("ceshi");
//        String name = "{\"msg\": {\"at\":1639375206553, \"imei\":\"869664048266707\", \"type\":1, \"ds_id\":\"320005750\", \"value\":\"4040030002FF001E10130B120000000004FF0000000000005E0002020284008100000000000046393136342D545F56312E30204E6F7620313620323031382031353A333400001E10130B1283008000000000000046393136342D545F56312E30204E6F7620313620323031382031353A333400001E10130B12232323\", \"devid\":862124562} , \"msg_signature\":\"xLGs4U0OyddVAGdAdF5TcQ==\", \"nonce\":\"wCG&1Hzq\"} ";
//        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
//        System.out.println(obj.getMsg());
////        devHisSXinService.putDevHisSXinToMQ(obj.getMsg());
//        devHisWaterService.putDevHisWaterToMQ(obj.getMsg());
//    }

//================================================================================================================

    /**
     * 功能描述：第三方平台数据接收。<p>
     *           <ul>注:
     *               <li>1.OneNet平台为了保证数据不丢失，有重发机制，如果重复数据对业务有影响，数据接收端需要对重复数据进行排除重复处理。</li>
     *               <li>2.OneNet每一次post数据请求后，等待客户端的响应都设有时限，在规定时限内没有收到响应会认为发送失败。
     *                    接收程序接收到数据时，尽量先缓存起来，再做业务逻辑处理。</li>
     *           </ul>
     * @param request 数据消息
     * @return 任意字符串。OneNet平台接收到http 200的响应，才会认为数据推送成功，否则会重发。
     */
    @RequestMapping(value = "/receive/water/top",method = RequestMethod.POST)
    @ResponseBody
    public String receivetopri(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
//        System.out.println("data receive:  body String --- " +name);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
//        System.out.println("data receive:  body Object --- " +obj);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
//            boolean dataRight = OneNetUtils.checkSignature(obj, topriToken);
            if (dataRight){
                System.out.println("data receive: content" + obj.toString());
//                devHisWaterService.putTopSoerDevHisWaterToMQ(obj.getMsg());
            }else {
                System.out.println("data receive: signature error");
            }

        }else {
            System.out.println("data receive: body empty error");
        }

        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     * @param msg 验证消息
     * @param nonce 随机串
     * @param signature 签名
     * @return msg值
     */

    @RequestMapping(value = "/receive/water/top", method = RequestMethod.GET)
    @ResponseBody
    public String checktopri(@RequestParam(value = "msg") String msg,
                        @RequestParam(value = "nonce") String nonce,
                        @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }

    //================================================================================================================

    /**
     * 功能描述：第三方平台数据接收。<p>
     *           <ul>注:
     *               <li>1.OneNet平台为了保证数据不丢失，有重发机制，如果重复数据对业务有影响，数据接收端需要对重复数据进行排除重复处理。</li>
     *               <li>2.OneNet每一次post数据请求后，等待客户端的响应都设有时限，在规定时限内没有收到响应会认为发送失败。
     *                    接收程序接收到数据时，尽量先缓存起来，再做业务逻辑处理。</li>
     *           </ul>
     * @param request 数据消息
     * @return 任意字符串。OneNet平台接收到http 200的响应，才会认为数据推送成功，否则会重发。
     */
    @RequestMapping(value = "/receive/water/top/yewei",method = RequestMethod.POST)
    @ResponseBody
    public String receivetopriyewei(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
//        System.out.println("data receive:  body String --- " +name);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
//        System.out.println("data receive:  body Object --- " +obj);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
//            boolean dataRight = OneNetUtils.checkSignature(obj, topriToken);
            if (dataRight){
                System.out.println("data receive: content" + obj.toString());
//                devHisWaterService.putTopSoerDevHisWaterToMQ(obj.getMsg());
            }else {
                System.out.println("data receive: signature error");
            }

        }else {
            System.out.println("data receive: body empty error");
        }

        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     * @param msg 验证消息
     * @param nonce 随机串
     * @param signature 签名
     * @return msg值
     */

    @RequestMapping(value = "/receive/water/top/yewei", method = RequestMethod.GET)
    @ResponseBody
    public String checktopriyewei(@RequestParam(value = "msg") String msg,
                             @RequestParam(value = "nonce") String nonce,
                             @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }
}
